home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / FRCOMP.ZIP / unfracpk.c < prev   
C/C++ Source or Header  |  1992-11-12  |  5KB  |  156 lines

  1.  
  2. Here is the decompression code I promised. (if you didn't get the compression
  3. code yet, its on its way)
  4. The .tga files are readable by Graphics Workshop.  Note that they are
  5. atleast 3 times the necessary size, but I wrote this a long time ago 
  6. before I know the specs for 256x256x256 .tga files.  GWS will handle it though.
  7. It will be updated in the next release at lyapunov.ucsd.edu (in fact it
  8. may be already)
  9. The .ifs files created with the version of fracpack I sent you are not 
  10. compatible with the ones at lyapunov. ( i think...)
  11. Enjoy.
  12.  
  13.  
  14. ----------------  Cut here ------------------
  15.  
  16.  
  17. /* This program decodes .IFS files into TARGA files */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <graphics.h>
  22. int main(int argc, char **argv)
  23. {
  24.    unsigned char domain [256][256], range [256][256];
  25.    FILE *in, *out;
  26.    char *inf, *outf, rmsstr[13];
  27.    int xsize=256, ysize=256, xhi=1, xlo=0, yhi=1, ylo=0;
  28.    int x, y, dx, dy, rx, ry, tsx, tsy;
  29.    int ii[32][32], ddxx[32][32], ddyy[32][32];
  30.    int transx[32][32], transy[32][32];
  31.    float ss[32][32], oo[32][32], z;
  32.    int ix, iy, iddxx, iddyy, n, i, nflips=8, niterations;
  33.    float s, o;
  34.    int f11[] = {1, 0, -1, 0, 1, 0, -1, 0}, f12[] = {0, 1, 0, -1, 0, -1, 0, 1};
  35.    int f21[] = {0, -1, 0, 1, 0, -1, 0, 1}, f22[] = {1, 0, -1, 0, -1, 0, 1, 0};
  36.    struct trans_t {
  37.       unsigned char dx;
  38.       unsigned char dy;
  39.       signed char scale;
  40.       short int offset : 13;
  41.       unsigned short int flip : 3;
  42.       } trans[1];
  43.  
  44.    if ((argc < 3)||(argc > 4)) {
  45.       printf("usage: unfracpk iterations infile.ext [outfile.ext]");
  46.       return 1;
  47.       }
  48.    niterations = atoi(argv[1]);
  49.    inf = argv[2]; if (argc == 4) outf = argv[3];
  50.    
  51.    if ((in = fopen(inf, "rb")) == NULL) {
  52.       fprintf(stderr, "Cannot open input file.\n");
  53.       return 1;
  54.       }
  55.    if (argc == 4) if ((out = fopen(outf, "wb")) == NULL) {
  56.       fprintf(stderr, "Cannot open output file.\n");
  57.       return 1;
  58.       }
  59.    GrSetMode(GR_default_graphics);
  60.    for (y = 0; y < ysize; y++)
  61.       GrSetColor(y,y,y,y);
  62.    for (ry = 0; ry < 32; ry++)
  63.    for (rx = 0; rx < 32; rx++)
  64.       {
  65.       fread(trans, sizeof(struct trans_t), 1, in);
  66.       ddxx[ry][rx] = dx = trans[0].dx;
  67.       ddyy[ry][rx] = dy = trans[0].dy;
  68.       ii[ry][rx] = i = trans[0].flip;
  69.       ss[ry][rx] = (trans[0].scale/127.) * 1.2;
  70.       oo[ry][rx] = trans[0].offset;
  71.       transx[ry][rx] = 2*8*rx + 7 - (f11[i]*(dx+7) + f12[i]*(dy+7));
  72.       transy[ry][rx] = 2*8*ry + 7 - (f21[i]*(dx+7) + f22[i]*(dy+7));
  73.       }
  74.    fclose(in);
  75.    printf("file loaded\n");
  76. /* Initialize 8-bit-grey-scale pixels in "from" array.  */
  77.    printf("Init Array\n");
  78.    for (y = 0; y < ysize; y++)
  79.    for (x = 0; x < xsize; x++)
  80.       {
  81.        GrPlot(x,y,127);
  82.        domain[y][x] = 127;
  83.       }
  84.    printf("\n\n");
  85.    for (n = 0; n < niterations; n++)
  86.    {
  87. /* Run through all non-overlapping 8x8 "R" blocks in the image  */
  88.     for (ry = 0; ry < 32; ry+=1)
  89.         {
  90.         GrLine(256, 8*ry, 256, 8*ry + 8, 340);
  91.         for (rx = 0; rx < 32; rx+=1)
  92.             {
  93.             s = ss [ry] [rx];
  94.             o = oo [ry] [rx];
  95.             i = ii [ry] [rx];
  96.             tsx = transx [ry] [rx];
  97.             tsy = transy [ry] [rx];
  98.             iddyy = ddyy [ry] [rx];
  99.             iddxx = ddxx [ry] [rx];
  100. /*************************************************************************/
  101. /*                Average & Transform the 256 2x2 "pixels"               */
  102. /*************************************************************************/
  103.             for (y = 0; y < 16; y+=2)
  104.             for (x = 0; x < 16; x+=2)
  105.                 {
  106.                 dy = iddyy + y;
  107.                 dx = iddxx + x;
  108.                 z = (float)((domain[dy  ][dx  ]
  109.                            + domain[dy  ][dx+1]
  110.                            + domain[dy+1][dx  ]
  111.                            + domain[dy+1][dx+1]) >> 2);
  112.  
  113.                 ix = (f11[i]*dx + f12[i]*dy + tsx) >> 1;
  114.                 iy = (f21[i]*dx + f22[i]*dy + tsy) >> 1;
  115.                 z = s*z + o;
  116.                 if (z > 255.) z = 255.; else if (z < 0.) z = 0.;
  117.                 range[iy][ix] = (unsigned char)z;
  118.                 if (range[iy][ix] != domain[iy][ix]) GrPlot(ix,iy,(int)z);
  119.                 }
  120.             }
  121.         }
  122.         for (y = 0; y < ysize; y++)
  123.         for (x = 0; x < xsize; x++)
  124.             domain[y][x] = range[y][x];
  125.     }
  126.  
  127. /* Write it out. */
  128.    if (argc == 4) {
  129.       if ((out = fopen(outf, "wb")) == NULL) {
  130.       fprintf(stderr, "Cannot open output file.\n");
  131.       return 1;
  132.       }
  133. /*Write header info.  This seems to work; I've yet to check TARGA specs. */
  134.    fputc(0, out); fputc(0, out);
  135.    fputc(2, out); fputc(0, out);
  136.    fputc(0, out); fputc(0, out);
  137.    fputc(0, out); fputc(0, out);
  138.    fputc(0, out); fputc(0, out);
  139.    fputc(0, out); fputc(0, out);
  140.    fputc(xlo, out); fputc(xhi, out);
  141.    fputc(ylo, out); fputc(yhi, out);
  142.    fputc(24, out); fputc(32, out);
  143.    
  144.  
  145.    for (y = 0; y < ysize; y++)
  146.    for (x = 0; x < xsize; x++)
  147.       {fputc(range[y][x],out); fputc(range[y][x],out); fputc(range[y][x],out);}
  148.    fclose(out); 
  149.    }
  150.    getch();
  151.    GrSetMode(GR_default_text);
  152. /* All done. Whew... */
  153.    return 0;
  154. }
  155.  
  156.